package Common
{
	import Base.Loader.SWFLoader;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;

	/**
	 * ...
	 * @author fox
	 */
	public class GameResManager
	{
		public var mainRes:Object = null;
		
		public var resMap:Object;
		public var loader:Object;
		
		public var loadCount:int = 0;
		
		public static var instance:GameResManager = null;
		public static function Instance():GameResManager
		{
			if ( instance == null )
			{
				instance = new GameResManager();
			}
			
			return instance;
		}
		
		
		public function init():void
		{
			resMap = new Object();
			loader = new Object();
		}
		
		
		public function release():void
		{
			resMap = null;
			loader = null;
		}
		
		
		public function getSWF( path:String ):Object
		{
			if ( resMap[ path ] )
			{
				return resMap[ path ].content;
			}
			
			return null;
		}
		
		
		public function getClass( path:String , name:String ):Class
		{
			if ( resMap[ path ] )
			{
				return resMap[ path ].getClass( name );
			}
			
			return null;
		}
		
		
		public function unload( path:String ):void
		{
			if ( loader[ path ] )
			{
				loader[ path ].loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
				loader[ path ].loader.Release();
				
				delete loader[ path ].loader;
				delete loader[ path ].fun;
				delete loader[ path ];
				
				//UILoading.instance.visible( false );
			}
		}
		
		public function load( path:String , fun:Function , loading:Boolean = true ):void
		{
			if ( loader[ path ] )
			{
				loader[ path ].fun.push( fun );
				return;
			}
			
			if ( resMap[ path ] )
			{
				fun( resMap[ path ] );
				return;
			}
			
			loadCount++;
			
			var l:SWFLoader = new SWFLoader();
			l.contentLoaderInfo.addEventListener( Event.INIT , onInit );
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR , onError );
			l.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS , onProgress );
			l.Load( path );
			
			loader[ path ] = new Object();
			loader[ path ].loader = l;
			loader[ path ].loading = loading;
			loader[ path ].fun = new Array();
			loader[ path ].fun.push( fun );
			
			if ( loading )
			{
				//
				//UILoading.instance.visible( true );
				//UILoading.instance.setText( 0 );
			}
		}
		
		
		public function onError( event:IOErrorEvent ):void
		{
			if ( loader[ event.target.loader.path ].loading )
			{
				//
				//UILoading.instance.visible( false );
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
			
			loadCount--;
		}
		
		
		public function onProgress( event:ProgressEvent ):void
		{
			if ( loader[ event.target.loader.path ].loading )
			{
				var num:int = event.bytesLoaded / event.bytesTotal * 100;
				//UILoading.instance.setText( num );
			}
			
		}
		
		
		public function addSWF( path:String , loader:SWFLoader ):void
		{
			resMap[ path ] = loader;
		}
		
		
		public function onInit( event:Event ):void
		{
			if ( loader[ event.target.loader.path ].loading )
			{
				//
				//UILoading.instance.visible( false );
			}
			
			resMap[ event.target.loader.path ] = loader[ event.target.loader.path ].loader;
			
			for ( var i:int = 0 ; i < loader[ event.target.loader.path ].fun.length ; i++ )
			{
				if ( loader[ event.target.loader.path ].fun[ i ] )
				{
					loader[ event.target.loader.path ].fun[ i ]( resMap[ event.target.loader.path ] );
				}
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
			
			loadCount--;
			
			
		}
		
	}
}
